package com.mailanalyzer.retrievers.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;

import com.mailanalyzer.exception.RetrievalException;
import com.mailanalyzer.retrievers.Retriever;
import com.mailanalyzer.utils.ConfigUtil;

/**
 * 
 * @author anibal.kolker
 *
 */
public class GmailRetriever implements Retriever {

	@Override
	public Message[] retrieve() throws RetrievalException {
		Properties props = System.getProperties();
		props.setProperty("mail.store.protocol", "imaps");
		
		try {
			Session session = Session.getDefaultInstance(props, null);
			Store store = session.getStore("imaps");
			store.connect("imap.gmail.com", ConfigUtil.getInstance().getProperty("username")   , ConfigUtil.getInstance().getProperty("password"));
			System.out.println(store);

			Folder inbox = store.getFolder("[Gmail]/All Mail");
			inbox.open(Folder.READ_ONLY);
			return inbox.getMessages();

		}
		catch(Exception e){
			e.printStackTrace();
			return new Message[]{};
		}
	}



}
